package com.hz.copyingsystem.controller;

import com.hz.common.utils.ResponseData;
import com.hz.common.utils.ResponsePage;
import com.hz.copyingsystem.entity.*;
import com.hz.copyingsystem.service.*;
import com.hz.copyingsystem.vo.CopyingSteleCalligraphyCalligrapherVo;
import com.hz.copyingsystem.vo.CopyingSteleCalligraphyStyleVo;
import com.hz.copyingsystem.vo.CopyingSteleCalligraphyYearsVo;
import com.hz.utils.FixedInformationName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2023-05-09 15:02:11
 */
@RestController
@RequestMapping("copyingSteleCalligraphy")
@Api(tags = "临摹碑帖")
public class CopyingSteleCalligraphyController {
    @Autowired
    private FixedInformationDicService fixedInformationDicService;

    @Autowired
    private CopyingSteleCalligraphyService copyingSteleCalligraphyService;

    @Autowired
    private CopyingSteleCalligraphyCharacterService copyingSteleCalligraphyCharacterService;

    @Autowired
    private RecommendSteleCalligraphyCollectionService recommendSteleCalligraphyCollectionService;

    @Autowired
    private CopyingSteleCalligraphySinglepageService copyingSteleCalligraphySinglepageService;

    @Autowired
    StringRedisTemplate redisTemplate;




    /**
     * 推荐碑帖集合获取
     */
    @ApiOperation("推荐碑帖集合获取")
    @PostMapping("/restelecallicollection/all")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtype", value = "种类。1推荐临摹碑帖2推荐欣赏碑帖,可用值:1,2", paramType = "query", allowableValues = "1,2", dataType = "int", required = true)
    })
    public ResponseData<List<RecommendSteleCalligraphyCollectionEntity>> restelecallicollectionAll(@RequestParam("rtype") Integer rtype) {
        List<RecommendSteleCalligraphyCollectionEntity> reSteleCalliCollectionList = recommendSteleCalligraphyCollectionService.queryCollectionsByType(rtype);
        return ResponseData.success(reSteleCalliCollectionList);
    }

    /**
     * 推荐碑帖获取
     */
    @ApiOperation("推荐碑帖获取")
    @PostMapping("/restelecalligraphy/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "rid", value = "推荐碑帖集合id", paramType = "query", dataType = "Long", required = true)
    })
    public ResponseData<ResponsePage<List<CopyingSteleCalligraphyEntity>>> restelecalligraphyGet(@RequestParam("curPage") Integer curPage,
                                                                                                 @RequestParam("pageSize") Integer pageSize,
                                                                                                 @RequestParam("rid") Long rid) {
        List<RecommendSteleCalligraphyEntity> recommendSteleCalligraphyList = recommendSteleCalligraphyCollectionService.restelecalligraphyGet(curPage, pageSize, rid);
        // 进行对象list转换
        List<CopyingSteleCalligraphyEntity> copyingSteleCalligraphyCharacterList = recommendSteleCalligraphyList.stream()
                .map(recommendSteleCalligraphy -> new CopyingSteleCalligraphyEntity(
                        recommendSteleCalligraphy.getSid(),
                        recommendSteleCalligraphy.getSteleCalligraphy(),
                        recommendSteleCalligraphy.getSteleCalligraphyThumbnail(),
                        recommendSteleCalligraphy.getSteleCalligraphyName(),
                        recommendSteleCalligraphy.getCalligraphyIntroduction(),
                        recommendSteleCalligraphy.getCalligrapherIntroduction(),
                        recommendSteleCalligraphy.getSorder(),
                        recommendSteleCalligraphy.getCreatetime()
                )).collect(Collectors.toList());

        ResponsePage<List<CopyingSteleCalligraphyEntity>> page = new ResponsePage<>(
                copyingSteleCalligraphyCharacterList,
                recommendSteleCalligraphyCollectionService.restelecalligraphyGetNum(rid),
                pageSize
        );

        return ResponseData.success(page);
    }


    /**
     * 临摹碑帖所有年代、书体、书家获取
     */
    @ApiOperation("临摹碑帖所有年代、书体、书家获取")
    @GetMapping("/ysc/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "查询类别。1年代，2书体，3书家", paramType = "query", dataType = "Integer", allowableValues = "1,2,3", required = true)
    })
    public ResponseData yscGet(@RequestParam("type") Integer type) {
        // 查询所有的年代、书体、书家名称
        List<FixedInformationDicEntity> fixedInformationDicList;
        // 映射成vo List
        switch (type){
            case 1 :
                fixedInformationDicList = fixedInformationDicService.queryByName(FixedInformationName.CO_ST_CALLI_YEARS);
                List<CopyingSteleCalligraphyYearsVo> copyingSteleCalligraphyYearsList = fixedInformationDicList.stream()
                        .map(fixedInformationDic -> new CopyingSteleCalligraphyYearsVo(fixedInformationDic.getId(),
                                fixedInformationDic.getFixedInformationContent(),
                                fixedInformationDic.getUpdatetime(),
                                fixedInformationDic.getIorder()))
                        .collect(Collectors.toList());
                return ResponseData.success(copyingSteleCalligraphyYearsList);
            case 2 :
                fixedInformationDicList = fixedInformationDicService.queryByName(FixedInformationName.CO_ST_CALLI_STYLE);
                List<CopyingSteleCalligraphyStyleVo> copyingSteleCalligraphyStyleList = fixedInformationDicList.stream()
                        .map(fixedInformationDic -> new CopyingSteleCalligraphyStyleVo(fixedInformationDic.getId(),
                                fixedInformationDic.getFixedInformationContent(),
                                fixedInformationDic.getUpdatetime(),
                                fixedInformationDic.getIorder()))
                        .collect(Collectors.toList());
                return ResponseData.success(copyingSteleCalligraphyStyleList);
            case 3 :
                fixedInformationDicList = fixedInformationDicService.queryByName(FixedInformationName.CO_ST_CALLI_CALLIGRAPHER);
                List<CopyingSteleCalligraphyCalligrapherVo> copyingStCalliCalligrapherList = fixedInformationDicList.stream()
                        .map(fixedInformationDic -> new CopyingSteleCalligraphyCalligrapherVo(fixedInformationDic.getId(),
                                fixedInformationDic.getFixedInformationContent(),
                                fixedInformationDic.getUpdatetime(),
                                fixedInformationDic.getIorder()))
                        .collect(Collectors.toList());
                return ResponseData.success(copyingStCalliCalligrapherList);
        }
        return ResponseData.success("类别参数错误！", false);
    }

    /**
     * 碑帖单页列表查询
     */
    @ApiOperation("碑帖单页列表查询")
    @PostMapping("/steleCalliCharacter/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "临摹碑帖id", paramType = "query", dataType = "Long", required = true),
    })
    public ResponseData<List<CopyingSteleCalligraphySinglepageEntity>> steleCalliCharacterList(@RequestParam("sid") Long sid){
        List<CopyingSteleCalligraphySinglepageEntity> copyingSteleCalligraphySinglepageList =  copyingSteleCalligraphySinglepageService.getBySid(sid);
        return ResponseData.success(copyingSteleCalligraphySinglepageList);
    }

    /**
     * 碑帖分页列表
     */
    @ApiOperation("碑帖分页列表")
    @GetMapping("/stelecalligraphy/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "condition", value = "模糊查询条件", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "yid", value = "年代id", paramType = "query", dataType = "Long", required = false),
            @ApiImplicitParam(name = "sid", value = "书体id", paramType = "query", dataType = "Long", required = false),
            @ApiImplicitParam(name = "cid", value = "书家id", paramType = "query", dataType = "Long", required = false)
    })
    public ResponseData<ResponsePage<List<CopyingSteleCalligraphyEntity>>> stelecalligraphyPage(@RequestParam("curPage") Integer curPage,
                                                                                                @RequestParam("pageSize") Integer pageSize,
                                                                                                @RequestParam(value = "condition",required = false) String condition,
                                                                                                @RequestParam(value = "yid",required = false) Long yid,
                                                                                                @RequestParam(value = "sid",required = false) Long sid,
                                                                                                @RequestParam(value = "cid",required = false) Long cid) {

        List<CopyingSteleCalligraphyEntity> copyingSteleCalligraphyList =copyingSteleCalligraphyService.querySteleCalligraphyByIds(curPage, pageSize, yid, sid, cid, condition);

        ResponsePage<List<CopyingSteleCalligraphyEntity>> page = new ResponsePage<>(copyingSteleCalligraphyList,
                copyingSteleCalligraphyService.querySteleCalligraphyNumByIds(yid, sid, cid, condition),
                pageSize);
        return ResponseData.success(page);
    }

}
